package com.thread.test1;

/**
 * @description: TODO 类描述
 * @author: YUANG
 * @date: 2022/3/6
 **/
public class InterruptTest3 {
    public static void main(String[] args) throws InterruptedException {
        MyThread t = new MyThread();
        t.start();
        Thread.sleep(1000);
        //main 中断 t catch (InterruptedException e)  t break
        //注意! 某线程被interrupted时, 它恰好在wait,sleep,occupied, 它就会抛出异常, isInterrupted() 也就不会被正常置为false,
        t.interrupt();
        System.out.println(t.isInterrupted());

    }

    static class MyThread extends Thread {
        @Override
        public void run() {
            int n = 0;
            while (!isInterrupted()) {
                System.out.print("isInterrupted: " + isInterrupted());
                System.out.println("n: " + n);
                n++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println("end: " + isInterrupted());
                    break;
                }
            }
        }
    }
}
